package com.huawei.chengdu.meetingdemo.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.device.idea.meeting.Addressbook.model.AddrBookUserInfo;
import com.huawei.chengdu.meetingdemo.R;
import com.huawei.chengdu.meetingdemo.presenter.ConfSdkPresenter;
import com.huawei.chengdu.meetingdemo.util.LogUtil;
import com.huawei.chengdu.meetingdemo.view.dialogs.ModifyContactDialog;

import java.util.ArrayList;
import java.util.List;

public class LocalContactListAdapter extends RecyclerView.Adapter<LocalContactListAdapter.ContactViewHolder> {
    public List<AddrBookUserInfo> selectedContactList = new ArrayList<>();
    private List<AddrBookUserInfo> contactList = new ArrayList<>();
    private Context mContext;

    public LocalContactListAdapter(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_contact_local, parent, false);
        return new ContactViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull LocalContactListAdapter.ContactViewHolder holder, int position) {
        AddrBookUserInfo addrBookUserInfo = contactList.get(position);
        holder.contactName.setText(addrBookUserInfo.getName());
        holder.contactNumber.setText(addrBookUserInfo.getLineInfo().getSiteNumber());
        // 只有本地通讯录支持修改、删除
        holder.contactCall.setOnClickListener(v -> ConfSdkPresenter.addressBookCall(addrBookUserInfo));
        holder.contactDelete.setOnClickListener(v -> deleteContactList(addrBookUserInfo.getId(), position));
        holder.contactEdit.setOnClickListener(v -> editContactList(addrBookUserInfo));
        holder.contactSelect.setOnCheckedChangeListener((buttonView, isChecked) ->
                handleContactSelect(isChecked, addrBookUserInfo));
    }

    @SuppressLint("NotifyDataSetChanged")
    public void updateContactList(List<AddrBookUserInfo> newContactList) {
        contactList.clear();
        contactList.addAll(newContactList);
        notifyDataSetChanged();
    }

    private void deleteContactList(int deleteId, int index) {
        int result = ConfSdkPresenter.deleteLocalUser(deleteId);
        if (result == 0) {
            contactList.remove(index);
        } else {
            LogUtil.error("delete local contact fail");
        }
    }

    private void editContactList(AddrBookUserInfo editUser) {
        ModifyContactDialog modifyContactDialog = new ModifyContactDialog(mContext, editUser);
        modifyContactDialog.show();
    }

    private void handleContactSelect(boolean isSelect, AddrBookUserInfo addrBookUserInfo) {
        if(isSelect) {
            selectedContactList.add(addrBookUserInfo);
        } else {
            selectedContactList.remove(addrBookUserInfo);
        }
    }

    @Override
    public int getItemCount() {
        return contactList.size();
    }

    public static class ContactViewHolder extends RecyclerView.ViewHolder {
        private final TextView contactName;
        private final TextView contactNumber;
        private final View contactCall;
        private final View contactEdit;
        private final View contactDelete;
        private final CheckBox contactSelect;

        public ContactViewHolder(@NonNull View itemView) {
            super(itemView);
            contactName = itemView.findViewById(R.id.local_contact_name);
            contactNumber = itemView.findViewById(R.id.local_contact_number);
            contactCall = itemView.findViewById(R.id.local_contact_call);
            contactEdit = itemView.findViewById(R.id.contact_edit);
            contactDelete = itemView.findViewById(R.id.contact_delete);
            contactSelect = itemView.findViewById(R.id.local_contact_select);
        }
    }

}
